package proxirabais.mobile.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import messages.SystemMessage;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import proxirabais.mobile.R;
import proxirabais.mobile.R.id;
import proxirabais.mobile.R.layout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import data.Config;

public class NewUserMarkerActivity extends Activity {
	
	private long latitude;
	private long longitude;
	
	
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.usermarker);
        
        Bundle extras = getIntent().getExtras();
        this.latitude = Long.parseLong(extras.getString(SystemMessage.PROMOTION_LATITUDE));
		this.longitude = Long.parseLong(extras.getString(SystemMessage.PROMOTION_LONGITUDE));

        final Button saveButton = (Button) findViewById(R.id.idSaveUserMarker);
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // poster l'object au serveur
            	EditText nom = (EditText)findViewById(R.id.idNomMarker);
            	EditText desc = (EditText)findViewById(R.id.idDescriptionMarker);
            	
            	sendPromotionToWebService(nom.getText().toString(),desc.getText().toString(),longitude,latitude);
            	finish();
            	
            }
        });
        
        final Button cancelButton = (Button) findViewById(R.id.idAnnulerUserMarker);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	 Intent intent = new Intent();
                 setResult(RESULT_OK, intent);
                 finish();
            }
        });
        
    }
    
    

	



	protected void sendPromotionToWebService(String nom, String desc, long lon, long lat) {
		// TODO Auto-generated method stub
		 // Create a new HttpClient and Post Header
	    HttpClient httpclient = new DefaultHttpClient();
	    HttpPost httppost = new HttpPost("http://"+Config.host+"/proxirabaisServer/PromotionServlet");

	    try {	        
	        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	        nameValuePairs.add(new BasicNameValuePair(SystemMessage.PROMOTION_NOM, nom));
	        nameValuePairs.add(new BasicNameValuePair(SystemMessage.PROMOTION_DESC, desc));
	        nameValuePairs.add(new BasicNameValuePair(SystemMessage.PROMOTION_LONGITUDE, lon+""));
	        nameValuePairs.add(new BasicNameValuePair(SystemMessage.PROMOTION_LATITUDE, lat+""));
	        nameValuePairs.add(new BasicNameValuePair(SystemMessage.ACTION, SystemMessage.ADD_NEW_USER_PROMOTION));
	        
	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

	        // Execute HTTP Post Request
	        HttpResponse response = httpclient.execute(httppost);
	        
	    } catch (ClientProtocolException e) {
	        Log.v("ERR",e.getMessage());
	    } catch (IOException e) {
	    	Log.v("ERR",e.getMessage());
	    }
	}
}
